home *** CD-ROM | disk | FTP | other *** search
- Path: news.ichange.com!newsmaster
- From: Jesse Liberty <jl@staff.ichange.com>
- Newsgroups: comp.lang.c++
- Subject: Re: NEWBIE : Quicksort
- Date: Wed, 27 Mar 1996 07:24:26 -0500
- Organization: AT&T
- Message-ID: <3159337A.2DB5@staff.ichange.com>
- References: <Pine.SOL.3.91.960324135520.8433A-100000@orion> <4j4ruf$gf4@news1.h1.usa.pipeline.com> <Pine.SOL.3.91.960325221334.202B-100000@orion>
- NNTP-Posting-Host: 140.244.99.60
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
- CC: jl@staff.ichange.com
-
- FRANCO wrote:
- >
- > On 25 Mar 1996, Pete Grant wrote:
- >
- > > int n = sizeof(z) / sizeof(int);
- > > qsort(z, n, sizeof(int), comp);
- > > for (int i = 0; i < n; i++)
- > > cout << ' ' << z[i];
- >
- > Pete,
- >
- > Can this be done without using qsort from the standard library <stdlib.h>?
-
- Sure, in my book Teach Yourself MORE C++ In 21 Days I show how to write a number of sorts, quicksort among them. Here's the
- basic code.
-
- template <class T>
- void myQuickSort(T* Input, int left, int right)
- {
- if (right > left)
- {
- T target = Input[right-1];
- int i = left-1;
- int x = right-1;
- for (;;)
- {
- while (*Input[++i] < *target)
- ;
- while (*Input[--x] > *target)
- ;
-
- if (i >= x)
- break;
- Swap(Input[i], Input[x]);
- }
-
- Swap(Input[i], Input[right-1]);
- myQuickSort(Input,left,i);
- myQuickSort(Input,i+1,right);
-
- }
- }
-
- template <class T>
- inline void Swap(T& i, T& j)
- {
- T temp;
- temp = j;
- j = i;
- i = temp;
- }
-
- ------
- Jesse Liberty [AT&T New Media Services]
- jl@staff.ichange.com ZDNet: 72241,72
- Teach Yourself C++ In 21 Days. Sams 1994
- Teach Yourself MORE C++ In 21 Days. Sams 1996
- Teach Yourself ANSI C++ In 21 Days. Sams 1996
- C++: An Introduction To Programming. Que 1996
-